<?php
class IndexAction extends Action {

    public function  __construct() {
        parent::__construct();
        $this->assign('domain',HTTP_DOMAIN);
    }

    /**
     * 网站首页
     */
    public function index() {
        $this->display();
    }

    /**
     * 用户登录后的页面
     */
    public function member() {
        $this->display();
    }


    /**
     * 显示假期情况
     */
    public function showh() {
        $uid=$_SESSION['user_id'];
        if($_GET['user_id']){
            $uid=$_GET['user_id'];
        }

        $a=D('LeaveTotal')->getLeaveTotalRemain(30,'user_id ASC',' AND user_id='.$uid);

        $this->assign('pager',$a['0']);
        $this->assign('list',$a[1]);
        $this->display();
    }


    /**
     * 展示请假历史记录
     */
    public function showlogs() {
        if($_GET['user_id']){
            $uid=$_GET['user_id'];
        }else{
			$uid=$_SESSION['user_id'];
		}
        $a=D('LeaveLog')->getLogs(30,'user_id ASC',' AND user_id='.$uid);
        $this->assign('pager',$a['0']);
        $this->assign('list',$a[1]);
        $this->display();
    }

    /**
     * 我的下属信息
     */
    public function subordinate(){
        $map['leader_user_id']=$_SESSION['user_id'];
        if($_GET['user_id']){
            $map['leader_user_id']=$_GET['user_id'];
        }
        //获取我所有的下属信息
        $list=D('User')->where($map)->select();
        if(!$list){
            $this->error('没有下属!');
        }
        foreach($list as &$v){
            $user_id=$v['user_id'];
            $m['leader_user_id']=$user_id;
            $sslist=D('User')->where($m)->findAll();
            if($sslist){
                $v['ss']=true;
            }else{
                $v['ss']=false;
            }
            unset($v);
        }
        $this->assign('list',$list);
        $this->display();
    }




    /**
     * 处理用户登录
     */
    public function doLogin() {
        $_SESSION['admin_user_id']=0;
        $userId=$_POST['user_id'];
        $lastName=$_POST['last_name'];
        $firstName=$_POST['first_name'];

        if($u=D('User')->checkUserNameId($firstName,$lastName,$userId)) {
            $_SESSION['user_is_logind']=1;
            $_SESSION['user_id']=$userId;
            $_SESSION['first_name']=$u['first_name'];
            $_SESSION['last_name']=$u['last_name'];
            $this->assign('waitSecond',10);
            $this->assign('jumpUrl',HTTP_DOMAIN.'/index.php/Index/member/');
            $this->success('Login Success!');
        }else {
            $this->error('Login Failed!');
        }
    }


	public function exporter(){
		
			$arr=$_POST['checkbox'];
			if(empty($arr)){
				$this->error('Empty!');
			}
			$arr=array_filter($arr);
			$str=implode(',',$arr);
			$map=" id in ($str)";
			if($_POST['type']=="showh"){
				echo 'Matricule;Nom;Prénom;ssType;Nombre;Décpte;'."\r\n";
				$r=D('LeaveTotal')->where($map)->findAll();
			}
			if($_POST['type']=="showlogs"){
				echo 'Matricule;Nom;Prénom;ssType;Début;Fin;Jours;'."\r\n";
				$r=D('LeaveLog')->where($map)->findAll();
			}

			if($_POST['type']=='subordinate'){
				$map=" user_id in ($str)";
				if($_POST['decompte']){
					echo 'Matricule;Nom;Prénom;ssType;Début;Fin;Jours;'."\r\n";
					$r=D('LeaveLog')->where($map)->findAll();
				}else{
					echo 'Matricule;Nom;Prénom;ssType;Nombre;Décpte;'."\r\n";
					$r=D('LeaveTotal')->where($map)->findAll();
				}

			}

			if(empty($r)){
				$this->error('Empty!');
			}

			$filename="Export_".date("Y_m_d_H_i_s").".csv";
            header("Content-Type: application/vnd.ms-excel; charset=UTF-8");   
			//header("Content-type:text/x-csv");  
            header("Pragma: public");   
            header("Expires: 0");   
            header("Cache-Control: must-revalidate, post-check=0, pre-check=0");   
                         
            //header("Content-Disposition: attachment;filename='".$Filename."'");   
			header('Content-Disposition: attachment; filename="'.$filename.'"');
            header("Content-Transfer-Encoding: binary ");


			if($_POST['type']=="showh"||($_POST['type']=='subordinate'&&!$_POST['decompte'])){
				foreach($r as $v){
					echo $v['user_id'].";".$v['first_name'].";".$v['last_name'].";".$v['type'].";".$v['total'].";".$v['remain'].";".$v['jours'];
					echo "\r\n";
				}
			}

			elseif($_POST['type']=="showlogs"||($_POST['type']=='subordinate'&&$_POST['decompte'])){
				foreach($r as $v){
					echo $v['user_id'].";".$v['first_name'].";".$v['last_name'].";".$v['type'].";".$v['debut'].";".$v['fin'].";".$v['jours'];
					echo "\r\n";
				}
			}

			else{
				foreach($r as $v){
					foreach($v as $k=>$vv){
						if($k!='id'){
							echo $vv.";";
						}
					}
					echo "\r\n";
				}	
			
			}
	}


}
?>